<template>
  <avue-crud
    ref="crudRef"
    v-model="dataForm"
    :option="tableOption"
    @close="visible = false"
    @submit="onSubmit"
  />
</template>

<script setup>
import { tableOption } from '@/crud/vip/vip.js'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const dataForm = ref({})

/**
 * 初始化
 * @param id
 */
const init = (id) => {
  visible.value = true
  dataForm.value = {}
  if (id) {
    getInfo(id)
  }
}

/**
 * 获取详情
 * @param id
 */
const getInfo = (id) => {
  http({
    url: http.adornUrl(`/admin/vip/${id}`),
    method: 'get'
  }).then(({ data }) => {
    dataForm.value = data
  })
}

/**
 * 表单提交
 * @param data
 * @param done
 */
const onSubmit = (data, done) => {
  http({
    url: http.adornUrl('/admin/vip'),
    method: data.id ? 'put' : 'post',
    data: http.adornData(data)
  }).then(() => {
    done()
    visible.value = false
    ElMessage.success('操作成功')
    emit('refreshDataList')
  })
}

defineExpose({
  init
})
</script>
